import React, { Component } from 'react';
import { delBillList } from '../../api'
class Index extends Component {
  state = {
    item: {}
  }
  async handleDelte() {
    const { item: { id } } = this.state
    const { history } = this.props
    const resp = await delBillList({ id })

    const { code, mess } = resp.data

    if(code ===200) {
      alert(mess)
      history.goBack()
    } else {
      alert('很遗憾！！！， 删除失败！')
    }
  }
  render() {
    const { item,  } = this.state
    const { history } = this.props
    console.log(history);
    // history 是路由导航对象
    //   history.push({ pathname: '路径', state: '传递的数据', search: '查询数据' })
    //   history.goBack()  后 退
    //   history.goForward() 前进
    //   history.go(num) num： 正数前进  num: 负数 后退 
    return (
      <div>
        <button onClick={ () => history.goBack() }>返回</button>
        <h2>名称：{ item.name } </h2>
        <h2>类型：{ item.type }</h2>
        <h2>金额：{ item.price }</h2>
        <h2>备注：{ item.tip }</h2>
        <button onClick={ () => this.handleDelte()  }>删除</button>
      </div>
    );
  }
  componentDidMount() {
    const { location } = this.props
    // location: 当前路由信息对象  
    // location.state  传递过来的数据
    // location.pathname 请求路径
    this.setState({
      item: location.state
    })
  }
}

export default Index;
